<?php
require_once ROOT_PATH.'include/IHandler.class.php';
/**
 * <h2>Handler工厂</h2>
 */
class HandlerFactory {
	/**
	 *
	 * @var HandlerFactory
	 */
	private static $_instance = null;
	
	/**
	 * <h2>构造函数</h2>
	 * <p>构造函数私有化，禁止直接创建工厂实例</p>
	 */
	private function __construct() {
	}
	
	/**
	 * <h2>获取工厂实例</h2>
	 *
	 * @return HandlerFactory
	 */
	public static function getSingleton() {
		if (empty ( self::$_instance )) {
			self::$_instance = new HandlerFactory ();
		}
		return self::$_instance;
	}
	
	/**
	 * <h2>创建一个Handler实例</h2>
	 *
	 * @param string $handlerName
	 *        	Handler类名
	 * @return IHandler
	 * @throws ExceptionEx
	 */
	public function createHandler($handlerName) {
		$handlerName = ucfirst ( $handlerName ) . 'Handler';
		$handlerFile = dirname(__FILE__).'/'.$handlerName.'.class.php';
		if (file_exists($handlerFile)) {
			require_once $handlerFile;
		} else {
			throw new ExceptionEx("Handler class File not exists");
		}
		if (class_exists ( $handlerName )) {
			return new $handlerName;
		}
		throw new ExceptionEx ( "Handler not found" );
	}
}